package design;

import java.util.concurrent.atomic.AtomicReference;

public class Singleton_06CAS线程安全 {

    private static final AtomicReference<Singleton_06CAS线程安全> INSTANCE = new AtomicReference<Singleton_06CAS线程安全>();

    private static Singleton_06CAS线程安全 instance;

    private Singleton_06CAS线程安全() {
    }

    public static final Singleton_06CAS线程安全 getInstance() {
        for (; ; ) {
            Singleton_06CAS线程安全 instance = INSTANCE.get();
            if (null != instance) return instance;
            INSTANCE.compareAndSet(null, new Singleton_06CAS线程安全());
            return INSTANCE.get();
        }
    }

    public static void main(String[] args) {
        System.out.println(Singleton_06CAS线程安全.getInstance()); // org.itstack.demo.design.Singleton_06@2b193f2d
        System.out.println(Singleton_06CAS线程安全.getInstance()); // org.itstack.demo.design.Singleton_06@2b193f2d
    }


}
